﻿// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace SnapObjects.Data.DB2
{
    internal abstract class DB2RoutineParameterSchemaProvider : RoutineParameterSchemaProvider
    {
        public DB2RoutineParameterSchemaProvider(DataContext dataContext) : base(dataContext)
        {
        }

        protected abstract bool IsProcedure { get; }

        protected override bool SkipReturnParameter => false;

        protected abstract string GetQueryString(RoutineNameInfo nameInfo, ISqlGenerationHelper helper);

        protected override IList<DynamicModel> QueryParameters(RoutineNameInfo nameInfo, ISqlGenerationHelper helper)
        {
            var sqlText = this.GetQueryString(nameInfo, helper);

            return _dataContext.SqlExecutor.Select<DynamicModel>(sqlText);
        }

        protected async override Task<IList<DynamicModel>> QueryParametersAsync(
            RoutineNameInfo nameInfo, ISqlGenerationHelper helper, CancellationToken cancellationToken)
        {
            var sqlText = this.GetQueryString(nameInfo, helper);

            return await _dataContext.SqlExecutor.SelectAsync<DynamicModel>(sqlText, cancellationToken);
        }
    }
}
